【转】Java中Collections.sort()和Arrays.sort()所采用的排序算法
http://121dog198.blog.163.com/blog/static/50859950201431661150523/
Java中如果需要对一个collections排序,需要继承于Comparable或者comparator接口,那么使用的排序算法是什么呢,一般情况下,排序算法包括:插入排序、快速排序、合并排序、冒泡排序等,java的Collections.sort算法调用的是合并排序,它是稳定排序,当数据接近有序的时候,效率更高,collections中的数据在排序前需要输入到array中,接着调用Arrays.sort函数来完成对象排序,最近通过迭代器将数组中排好序的对象些人到collection中,这也要求collection必须为mutable类型的。合并排序的大致过程为: void mergerSort(int[] a){ int len = a.lenght() int mid = len>>2 if(len>1){ int[] pre=a[0:mid); int[] after=a[mid:len); mergerSort(pre); mergerSort(after); merge(a,pre,after) } } 1.collections转化为array,并借助于arrays的sort功能完成排序,并回写到collection public static <T> void sort(List<T> list, Comparator<? super T> c) { Object[] a = list.toArray(); Arrays.sort(a, (Comparator)c); ListIterator i = list.listIterator(); for (int j=0; j<a.length; j++) { i.next(); i.set(a[j]); } } 2. Arrays合并排序的实现: public static <T> void sort(T[] a, Comparator<? super T> c) { if (LegacyMergeSort.userRequested) legacyMergeSort(a, c); else TimSort.sort(a, c); } /** To be removed in a future release. */ private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) { T[] aux = a.clone(); if (c==null) mergeSort(aux, a, 0, a.length, 0); else mergeSort(aux, a, 0, a.length, 0, c); } private static void mergeSort(Object[] src, Object[] dest, int low, int high, int off) { int length = high - low; // Insertion sort on smallest arrays if (length < INSERTIONSORT_THRESHOLD) { for (int i=low; i<high; i++) for (int j=i; j>low && ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--) swap(dest, j, j-1); return; } // Recursively sort halves of dest into src int destLow = low; int destHigh = high; low += off; high += off; int mid = (low + high) >>> 1; mergeSort(dest, src, low, mid, -off); mergeSort(dest, src, mid, high, -off); // If list is already sorted, just copy from src to dest. This is an // optimization that results in faster sorts for nearly ordered lists. if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) { System.arraycopy(src, low, dest, destLow, length); return; } // Merge sorted halves (now in src) into dest for(int i = destLow, p = low, q = mid; i < destHigh; i++) { if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0) dest[i] = src[p++]; else dest[i] = src[q++]; } } 注>>:二进制右移,左侧补符号位,>>>:二进制右移,左侧补无符号为,也就是0 3.举例: public class TestCompare { private String com; private int id; public TestCompare(int id, String com) { super(); this.com = com; this.id = id; } @Override public String toString() { return "TestCompare [com=" + com + ", id=" + id + "]"; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub List<TestCompare> li = new ArrayList<TestCompare>(); li.add(new TestCompare(1, null)); li.add(new TestCompare(2, "dfsd")); li.add(new TestCompare(3, null)); li.add(new TestCompare(4, "ying")); Collections.sort(li, new Comparator<TestCompare>() { @Override public int compare(TestCompare o1, TestCompare o2) { // TODO Auto-generated method stub if (o1.com == o2.com) return 0; else if (o1.com == null) return 1; else if (o2.com == null) return -1; else return o1.com.compareTo(o2.com); } }); List中含有4个元素,根据合并排序的算法,首先分为[0:2) 和[2:4) 接着[0,2)分为[0:1) 和[1:2) [0:1):TestCompare [com=null, id=1] [1:2):TestCompare [com=dfsd, id=2] 合并排序后为 TestCompare [com=dfsd, id=2] TestCompare [com=null, id=1] 接着执行[2:4),分为[2:3) 和[3:4) [2:3):TestCompare [com=null, id=3] [3:4):TestCompare [com=ying, id=4] 合并排序后为: TestCompare [com=ying, id=4] TestCompare [com=null, id=3] 将两组合并的数据进行再次合并,及为: TestCompare [com=dfsd, id=2] TestCompare [com=ying, id=4] TestCompare [com=null, id=1] TestCompare [com=null, id=3]
补充:Arrays.sort() 采用了2种排序算法 -- 基本类型数据使用快速排序法,对象数组使用归并排序。